package com.yonghong.groupreport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.yonghong.groupreport.config.DynamicDataSource;
import com.yonghong.groupreport.domain.PaOrganize;
import com.yonghong.groupreport.domain.PaReportline;
import com.yonghong.groupreport.service.IPaOrganizeService;
import com.yonghong.groupreport.utils.AjaxResult;
import com.yonghong.groupreport.utils.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yonghong.groupreport.domain.PaReportlineset;
import com.yonghong.groupreport.service.IPaReportlinesetService;


/**
 * 报表行设置信息Controller
 * 
 * @author yh
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/reports/reportlineset")
public class PaReportlinesetController extends BaseController
{
    @Autowired
    private IPaReportlinesetService paReportlinesetService;
    @Autowired
    private DynamicDataSource dynamicDataSource;
    @Autowired
    private IPaOrganizeService paOrganizeService;

    /**
     * 查询报表行设置信息列表
     */

    @GetMapping("/list")
    public AjaxResult list(PaReportlineset paReportlineset)
    {

        List<PaReportlineset> list = paReportlinesetService.selectPaReportlinesetList(paReportlineset);
        return success(list);
    }



    /**
     * 获取报表行设置信息详细信息
     */

    @GetMapping(value = "/{paReportlinesetId}")
    public AjaxResult getInfo(@PathVariable("paReportlinesetId") Long paReportlinesetId)
    {
        return success(paReportlinesetService.selectPaReportlinesetByPaReportlinesetId(paReportlinesetId));
    }

    /**
     * 新增报表行设置信息
     */

    @PostMapping
    public AjaxResult add(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException {
        int rows=0;
        if(ajaxResult.get("msg").toString()!=null)
        {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }
        PaReportlineset paReportlineset= JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")),PaReportlineset.class);

        if(ajaxResult.get("msg").equals("NC"))
        {
            paReportlineset.setPkCorp(paOrganizeService.selectOrgAccId(paReportlineset.getOrgId()));
            rows=paReportlinesetService.insertPaReportlinesetOracle(paReportlineset);
        }
        else {
            rows=paReportlinesetService.insertPaReportlineset(paReportlineset);
        }
        return toAjax(rows);
//        return toAjax(paReportlinesetService.insertPaReportlineset(paReportlineset));
    }

    /**
     * 修改报表行设置信息
     */

    @PutMapping
    public AjaxResult edit(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException {
        int rows=0;
        if(ajaxResult.get("msg").toString()!=null)
        {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }
        PaReportlineset paReportlineset= JSON.parseObject(JSON.toJSONString(ajaxResult.get("data")),PaReportlineset.class);
        if(ajaxResult.get("msg").equals("NC"))
        {
            paReportlineset.setPkCorp(paOrganizeService.selectOrgAccId(paReportlineset.getOrgId()));
            rows=paReportlinesetService.updatePaReportlinesetOracle(paReportlineset);
        }
        else {
            rows=paReportlinesetService.updatePaReportlineset(paReportlineset);
        }
        return toAjax(rows);
       // return toAjax(paReportlinesetService.updatePaReportlineset(paReportlineset));
    }

    /**
     * 删除报表行设置信息
     */

	@DeleteMapping
    public AjaxResult remove(@RequestBody AjaxResult ajaxResult) throws NoSuchFieldException, IllegalAccessException, JSONException {
        if (ajaxResult.get("msg") != null) {
            DynamicDataSource.clearDataSource();
            // 切换数据源
            dynamicDataSource.createDataSource(ajaxResult.get("msg").toString());
        }

        JSONArray jsonArr = new JSONArray(ajaxResult.get("data").toString()); // 创建JSONArray对象并传入JSON字符串

        int length = jsonArr.length(); // 获取JSON数组长度
        Long[] paReportlinesetIds = new Long[length]; // 定义存放结果的long类型数组

        for (int i = 0; i < length; i++) {
            paReportlinesetIds[i] = jsonArr.getLong(i); // 遍历JSON数组，依次提取每个元素并赋值到long数组中
        }

        return toAjax(paReportlinesetService.deletePaReportlinesetByPaReportlinesetIds(paReportlinesetIds));
    }
}
